<?php

/**
 * Class Weixin_Wfans_module
 */
class Weixin_Wfans_module extends CI_Module
{
    public $weixin_model;
    public $setting;

    public function __construct()
    {
        parent::__construct();
        $this->load->model('weixin.weixin_model');
        $this->load->model('expert.Expert_model');
        $this->setting = $this->weixin_model->db_get_setting();
        define('TOKEN', @$this->setting['token']);
        define('APPID', @$this->setting['appid']);
        define('APPSECRET', @$this->setting['appsecret']);

        $this->weixin_model->valid();
    }

    public function index()
    {
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            if($_POST['groupid'] && $_POST['openids']){
                $groupid = intval($_POST['groupid']);
                $this->weixin_model->move_group($groupid, $_POST['openids']);

            }else if( $groups = $_POST['group_id']){
                foreach ($groups as $openid => $groupid) {
                    $data[$groupid][] = $openid;
                }
                foreach ($data as $groupid => $openids) {
                    $this->weixin_model->move_group($groupid, $openids);
                }
            }

            $this->success('保存成功', current_url(), true);
        }

        $fans = $this->weixin_model->get_fanslist();
        $groups = $this->weixin_model->get_user_group();

        $page_config['perpage'] = 20;   //每页条数
        $page_config['url']     ='weixin/wfans/index';//url
        $page_config['param']   = '';//参数
        $page_config['seg']     =4;//参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
        $page_config['nowindex']=$this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']):1;//当前页

        $openids = array_slice($fans['data']['openid'], ($page_config['nowindex']-1)*$page_config['perpage'], $page_config['perpage']);
        $fansinfo = $this->weixin_model->get_batch_fansinfo($openids);
        $page_config['total']   = $fans['count'];

        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);


        $this->data['fans'] = $fans;
        $this->data['fansinfo'] = $fansinfo;

        foreach ($groups['groups'] as $k => $v) {
            $g[$v['id']] = $v;
        }
        $this->data['groups'] = $g;

        $this->data['title'] = '粉丝管理';
        $this->data['view'] = 'fans';
        $this->loadview($this->data, 'xenon/common/index');
    }

    public function group()
    {
        $groups = $this->weixin_model->get_user_group();
        foreach ($groups['groups'] as $k => $v) {
            $g[$v['id']] = $v;
        }

        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $updates = array();
            $submit = $_POST['group'];
            foreach ($submit as $gid => $name) {
                if($g[$gid]['name'] != trim($name)){
                    $updates[$gid] = trim($name);
                }
            }

            if($updates){
                foreach ($updates as $k => $v) {
                    $this->weixin_model->update_group($k, $v);
                }
            }
            if($newgroup = trim($_POST['newgroup'])){
                $this->weixin_model->create_group($newgroup);
            }
            if($delid = intval($_REQUEST['delid'])){
                $this->weixin_model->delete_group($delid);
            }

            $this->success('保存成功', current_url(), true);
        }
        $this->data['groups'] = $g;

        $this->data['title'] = '分组管理';
        $this->data['view'] = 'fansgroup';
        $this->loadview($this->data, 'xenon/common/index');
    }
}